package com.recharge.srevice;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.ChildMeter;
import com.entity.Customer;
import com.entity.Recharge;
import com.file.service.CustomerService;
import com.interfaces.ChildMeterDao;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.MeterDao;
import com.interfaces.RechargeDao;
import com.interfaces.SysdicDao;
import com.recharge.entity.JsonRecharge;
import com.recharge.entity.JsonRechargeCustomer;
import com.setting.service.SysdicService;
import com.utils.JsonResult;

/**
 * @Title: RefundService.java
 * @Package com.recharge.srevice
 * @author 孙梦楠
 * @date 2017年8月28日 上午11:15:06
 * @version V1.0.0
 */
@Service
public class RefundService {

	@Autowired
	private RechargeDao rechargedao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private CustomerService customerservice;

	@Autowired
	private SysdicDao sysdicdao;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private RechargeService rechargeservice;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private ChildMeterDao childmeterdao;

	// 列出可退费用户列表
	@Transactional
	public JsonResult<List<JsonRechargeCustomer>> list_refund_customer(String opeorgcookie, String opetypecookie,
			String customeraccount, String customername, String customeraddress, String languagecookie,
			String childtotalstatus) {

		List<Customer> customers = new ArrayList<Customer>();

		customers = customerdao.findByConditions_recharge(customeraccount, customername, customeraddress,
				Integer.valueOf(opeorgcookie), opetypecookie, "2", "2");
		List<JsonRechargeCustomer> jsonRechargeCustomers = new ArrayList<JsonRechargeCustomer>();

		for (int i = 0; i < customers.size(); i++) {

			JsonRechargeCustomer jsonRechargeCustomer = new JsonRechargeCustomer();
			Customer customer = customers.get(i);
			Integer customerid = customers.get(i).getCustomerid();
			jsonRechargeCustomer.setCustomerid(customerid);
			jsonRechargeCustomer.setCustomeraccount(customer.getCustomeraccount());
			jsonRechargeCustomer.setCustomername(customer.getCustomername());
			jsonRechargeCustomer.setCustomeraddress(customer.getCustomeraddress());
			jsonRechargeCustomer.setCustomerorg(customer.getCustomerorg());
			jsonRechargeCustomer.setCustomertype(customer.getCustomertype());
			jsonRechargeCustomer.setCustomerorgname(
					sysdicservice.finddisplay("0001", customer.getCustomerorg().toString(), languagecookie));
			// 查找最新一条
			if (rechargedao.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "退费", 0)
					.size() == 0) {
				jsonRechargeCustomer.setFrequency(0);
			} else {
				Recharge recharge = rechargedao
						.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "退费", 0).get(0);
				// 退费次数
				jsonRechargeCustomer.setFrequency(recharge.getFrequency());
			}
			// 剩余金额 通过用户ID查表ID 通过表ID查剩余金额
			Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();

			Double beforemoney = meterdao.findOne(meterid).getMetermoney();
			jsonRechargeCustomer.setMeterid(meterid);
			jsonRechargeCustomer.setBeforemoney(beforemoney);
			// 总分关系
			ChildMeter cm = childmeterdao.findByMeterid(meterid);

			if (childtotalstatus.equals("总")) {
				// 如果已设置表而且是总表
				if (cm != null && cm.getFathermeterid() == 0) {
					jsonRechargeCustomers.add(jsonRechargeCustomer);
				}
			} else {
				// 如果是未设置表或者是分表
				if (cm == null// 未设置表
						|| (cm != null && cm.getFathermeterid() == null)// 设置分表但是未关联总表
						|| (cm != null && cm.getFathermeterid() != 0)// 关联过的分表
				) {
					jsonRechargeCustomers.add(jsonRechargeCustomer);
				}
			}

		}

		return new JsonResult<List<JsonRechargeCustomer>>(541, "列出可退费用户", jsonRechargeCustomers);
	}

	// 退费
	@Transactional
	public JsonResult<Recharge> refund(Integer customerid, String opecodecookie, Double money, Double lastmoney) {

		if (money == null) {
			return new JsonResult<Recharge>(543, "退费失败..金额错误", null);

		} else if (lastmoney != null && lastmoney < money) {
			return new JsonResult<Recharge>(544, "退费失败..余额不足", null);
			// 按照id查，去掉开户size是0，证明刚开户。或者倒序查第一条不是购电
		} else if (rechargedao.findByCustomeridAndFrequencyNotOrderByFrequencyDesc(customerid, 0).size() == 0
				|| !(rechargedao.findByCustomeridAndFrequencyNotOrderByFrequencyDesc(customerid, 0).get(0)
						.getDirection().equals("购电"))) {
			return new JsonResult<Recharge>(546, "必须先充值后退费", null);
		} else {

			Recharge recharge = new Recharge();
			recharge.setCustomerid(customerid);
			Customer customer = customerdao.findOne(customerid);
			Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
			recharge.setMeterid(meterid);
			recharge.setCustomeraccount(customer.getCustomeraccount());
			recharge.setCustomername(customer.getCustomername());
			recharge.setCustomeraddress(customer.getCustomeraddress());
			recharge.setCustomerorg(customer.getCustomerorg());
			recharge.setCustomertype(customer.getCustomertype());
			recharge.setDirection("退费");
			recharge.setOpecode(opecodecookie);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String issuetime = sdf.format(new Date());
			recharge.setIssuetime(issuetime);
			if (rechargedao.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "退费", 0)
					.size() == 0) {
				recharge.setFrequency(1);
			} else {
				Integer frequency = rechargedao
						.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "退费", 0).get(0)
						.getFrequency();
				frequency++;
				recharge.setFrequency(frequency);
			}
			/*
			 * beforemoney 调用接口 aftermoney 接口
			 */

			recharge.setMoney(money);
			// Integer totalmoney =
			// rechargedao.findByCustomeridAndDirectionOrderByFrequencyDesc(customerid,"退费").get(0).getTotalmoney();
			Double totalmoney = 0.00;
			recharge.setTotalmoney(totalmoney);

			recharge.setOptstatus("等待下发");
			recharge.setOptnum(0);

			rechargedao.save(recharge);
			return new JsonResult<Recharge>(542, "退费命令已下发", recharge);
		}
	}

	// 退费历史记录
	@Transactional
	public JsonResult<List<JsonRecharge>> refundhistory(Integer customerid, String languagecookie) {

		List<Recharge> recharges = rechargedao
				.findByCustomeridAndDirectionAndFrequencyNotOrderByFrequencyDesc(customerid, "退费", 0);
		List<JsonRecharge> rechargehistories = rechargeservice.list_history(recharges, languagecookie);

		return new JsonResult<List<JsonRecharge>>(545, "退费历史记录", rechargehistories);
	}

	// 退租 清空空用户信息
	@Transactional
	public void unrent(Integer customerid) {

		customerdao.updateCustomernameAndphoneAndwechat(null, null, null, customerid);
	}

}
